#include "Extra.h"


void GPIO_Init(void){
    RCC->AHB1ENR |= (0x01<<1);
    RCC->APB1ENR |= (0x01<<2);
    
    GPIOB->MODER |= (0xAA<<12); //复用
    
    GPIOB->AFR[1] |= (0x22);//选择AF2，复用为TIM4
    
    GPIOB->AFR[0] |= (0x22<<24);//选择AF2，复用为TIM4 
}

void TIM4_Init(void){
    GPIO_Init();
    

    TIM4->CR1 &= ~(0xFFFF);
    TIM4->CR1 |= (0x01<<7);//ARR寄存器缓冲开启
    TIM4->CR1 |= (0x01<<2);//更新请求源，只有计数器上下溢才能生成更新中断或者DMA请求，也就是选择UEV事件源
    
    TIM4->CR2 &= ~(0xFFFF);//第七位TI1S保持0，CHANNEL1接入TI1输入
    
    TIM4->DIER &= ~(0xFFFF);
    TIM4->DIER |= (0x01); //使能更新中断
    TIM4->DIER |= (0x0F<<1); //使能各通道捕获/比较中断
    
    TIM4->EGR &= ~(0xFFFF);
    TIM4->EGR |= (0x0F<<1); //各通道捕获/比较事件生成使能，配置为输入时，各通道CCR寄存器将捕获到计数器当前值
    
    TIM4->CCMR1 &= ~(0xFFFF); 
    TIM4->CCMR1 |= (0x01); //通道一配置为输入，IC1映射到TI1上
    TIM4->CCMR1 |= (0x01<<8); // 通道二配置为输入，IC2映射到TI2上
    TIM4->CCMR2 &= ~(0xFFFF);
    TIM4->CCMR2 |= (0x01);
    TIM4->CCMR2 |= (0x01<<8);
    
    TIM4->CCER &= ~(0xFFFF);
    TIM4->CCER |= (0x1111);//使能捕获，最开始由上升沿触发
    
    TIM4->PSC = 84-1;//42MHz÷84 = 500KHz
    TIM4->ARR = 2000-1;//每次UEV产生500Hz，2ms产生一次也就是输出的PWM波频率为500Hz
    
    HAL_NVIC_SetPriority(TIM4_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(TIM4_IRQn);
    
    TIM4->CR1 |= 0x01;//开启计数器
}

int count_1=0,flag_1=0,T1,T1_s;
int count_2=0,flag_2=0,T2,T2_s;
int count_3=0,flag_3=0,T3,T3_s;
int count_4=0,flag_4=0,T4,T4_s;
int overflow1=0;
int overflow2=0;
int overflow3=0;
int overflow4=0;

void TIM4_IRQHandler(void){
    #if OS_CRITICAL_METHOD == 3u
        OS_CPU_SR cpu_sr;
    #endif
    OS_ENTER_CRITICAL();
    OSIntEnter();
    OS_EXIT_CRITICAL();
    
     if(TIM4->SR & (0x01<<0)){
        TIM4->SR &= ~(0x01<<0);
        if((flag_1 == 1) && (!(TIM4->SR & (0x01<<1)))) overflow1++;
        if((flag_2 == 1) && (!(TIM4->SR & (0x01<<2)))) overflow2++;
        if((flag_3 == 1) && (!(TIM4->SR & (0x01<<3)))) overflow3++;
        if((flag_4 == 1) && (!(TIM4->SR & (0x01<<4)))) overflow4++;
     }
     
     if (TIM4->SR & (0x1<<1)){
         TIM4->SR &= ~(0x01<<1);
        if(!flag_1){
            flag_1=1;
            T1_s=TIM4->CCR1;
            TIM4->CCER |= (0x01<<1);
        }
        else {
            flag_1=0;
            T1=TIM4->CCR1;
            TIM4->CCER &= ~(0X01<<1);
            count_1=(TIM4->ARR+1)*overflow1+T1-T1_s;
            overflow1=0;
        }
     }
     
     if (TIM4->SR & (0x1<<2)){
         TIM4->SR &= ~(0x01<<2);
        if(!flag_2){
            flag_2=1;
            T2_s=TIM4->CCR2;
            TIM4->CCER |= (0x01<<5);
        }
        else {
            flag_2=0;
            T2=TIM4->CCR2;
            TIM4->CCER &= ~(0X01<<5);
            count_2=(TIM4->ARR+1)*overflow2+T2-T2_s;
            overflow2=0;
            
        }
     }
     
     if (TIM4->SR & (0x1<<3)){//
         TIM4->SR &= ~(0x01<<3);
        if(!flag_3){
            flag_3=1;
            T3_s=TIM4->CCR3;
            TIM4->CCER |= (0x01<<9);
        }
        else {
            flag_3=0;
            T3=TIM4->CCR3;
            TIM4->CCER &= ~(0X01<<9);
            count_3=(TIM4->ARR+1)*overflow3+T3-T3_s;
            overflow3=0;

        }
     }
//     
     if (TIM4->SR & (0x1<<4)){//
         TIM4->SR &= ~(0x01<<4);
        if(!flag_4){
            flag_4=1;
            T4_s=TIM4->CCR4;
            TIM4->CCER |= (0x01<<13);
        }
        else {
            flag_4=0;
            T4=TIM4->CCR4;
            TIM4->CCER &= ~(0X01<<13);
            count_4=(TIM4->ARR+1)*overflow4+T4-T4_s;
            overflow4=0;

        }
     }
//     
    OSIntExit();
}
